<?php
namespace app\crontab\controller;
use app\common\library\CommonFun;
use app\common\model\statistics\CmsDataCountDayModel;
use app\common\model\user\UserCoinModel;
use app\common\redis\PythonPushRedis;
use app\common\service\CmsDataCountDayTypeConfig;

/**
 * 数据按天统计
 */
class Cms_data_count_day extends CrontabCommon
{
    /**
     * 初始化方法
     */
    protected function _initialize()
    {
        //先执行父类初始化
        parent::_initialize();
    }

    /**
     * 用户持仓人均点击数按天统计
     */
    public function user_read_avg_list()
    {
        $time = date('Y-m-d',strtotime("-1 day"));

        $data = CmsDataCountDayTypeConfig::user_read_avg_list( $time );

        $countModel = new CmsDataCountDayModel();

        $where = array(
            'count_time' => $time,
            'count_type' => CmsDataCountDayTypeConfig::user_read_avg_list
        );


        $data = array(
            'count_time'  => $time ,
            'count_type'  => CmsDataCountDayTypeConfig::user_read_avg_list,
            'data'        => CommonFun::json_encode2( $data ),
        );

        // 检测数据是否已经添加
        $id = $countModel->field('id')->where( $where )->find();


        if ( $id ) {
            $countModel->update_data( $id['id'], $data);
        } else {
            $countModel->add_data( $data );
        }

        echo '同步完成'.PHP_EOL;
    }

    /**
     * 用户持仓人均点击数按天统计初始化
     */
    public function user_read_avg_list_init()
    {
        $modelUserSta = new UserStatisticsMongo();
        $apiModel = new ApiLogMongo();
        $modelUserCoin = UserCoinModel::getInstance();

        $modelUserSta->where_eq("is_register", 1);
        $modelUserSta->field( array( 'create_time', 'is_asset', 'user_id' ) );
        $res = $modelUserSta->get();

        // 返回数据
        $data_list = array();

        if ( $res ) {
            $user_data_list = array();
            $active_user_id_list = array();
            $active_asset_user_list = array();
            $active_noasset_user_list = array();
            foreach ( $res as $v ) {
                $date = date("Y-m-d", $v["create_time"]);
                if ( !isset($user_data_list[$date][$v['is_asset']][$v['user_id']]) ) {
                    $user_data_list[$date][$v['is_asset']][$v['user_id']] = 0;
                }
                $user_data_list[$date][$v['is_asset']][$v['user_id']]++;
                // 当日活跃用户
                if ( !isset($active_user_id_list[$date]) ) {
                    $active_user_id_list[$date] = $apiModel->get_active_user_id_list_by_date( $date );
                }
                if ($active_user_id_list[$date]) {
                    $date_time = date("Y-m-d H:i:s", $v["create_time"]);
                    $where = array(
                        'user_id' => array('in', $active_user_id_list[$date]),
                        'create_time' => array( 'elt', $date_time ),
                    );
                    $asset_user_id_list = $modelUserCoin->search_id_list_no_page($where, 'user_id');
                    $active_asset_user_list[$date] = array_merge($active_asset_user_list[$date], $asset_user_id_list);
                    $noasset_user_id_list = array_diff($active_user_id_list[$date], $asset_user_id_list);
                    $active_noasset_user_list[$date] = array_merge($active_noasset_user_list[$date], $noasset_user_id_list);
                }
// 				foreach ( $active_user_id_list[$date] as $_acuid ) {
// 					//$date_time = date("Y-m-d H:i:s");
// 					$date_time = date("Y-m-d H:i:s", $v["create_time"]);
// 					$ac_is_asset = $modelUserCoin->before_date_is_asset($_acuid, $date_time);
// 					$ac_is_asset ? $active_asset_user_list[$date][] = $_acuid : $active_noasset_user_list[$date][] = $_acuid;
// 				}
                // 当日(并且是当前时间点上)
                if ( $v['is_asset'] ) {
                    // 持仓用户
                    $active_asset_user_list[$date][] = $v['user_id'];
                }else {
                    // 未持仓用户
                    $active_noasset_user_list[$date][] = $v['user_id'];
                }
            }
            foreach ( $user_data_list as $_date=>$_v ) {
                if ( !isset($data_list[$_date]) ) {
                    $data_list[$_date] = array(
                        "date" => $_date,
                        // 持仓用户总数
                        "asset_user_count"=>0,
                        // 持仓用户点击数量
                        "asset_count" => 0,
                        // 持仓用户人均点击次数
                        "asset_avg" => '',
                        // 非持仓用户总数
                        "noasset_user_count" => 0,
                        // 非持仓用户点击数量
                        "noasset_count" => 0,
                        // 非持仓用户人均点击次数
                        "noasset_avg" => '',
                    );
                }
                foreach ( $_v as $is_asset=>$_user_article ) {
                    if ( $is_asset ) {
                        // 持仓用户总数
                        //$data_list[$_date]['asset_user_count'] = count($_user_article);
                        $data_list[$_date]['asset_user_count'] = count(array_unique($active_asset_user_list[$_date]));
                        // 持仓用户点击数量
                        $data_list[$_date]['asset_count'] = array_sum($_user_article);
                        // 持仓用户人均点击次数
                        $data_list[$_date]['asset_avg'] = bcdiv($data_list[$_date]['asset_count'], $data_list[$_date]['asset_user_count'], 2);
                    }else {
                        // 非持仓用户总数
                        //$data_list[$_date]['noasset_user_count'] = count($_user_article);
                        $data_list[$_date]['noasset_user_count'] = count(array_unique($active_noasset_user_list[$_date]));
                        // 非持仓用户点击数量
                        $data_list[$_date]['noasset_count'] = array_sum($_user_article);
                        // 非持仓用户人均点击次数
                        $data_list[$_date]['noasset_avg'] = bcdiv($data_list[$_date]['noasset_count'], $data_list[$_date]['noasset_user_count'], 2);
                    }
                }
            }
        }

        $countModel = new CmsDataCountDayModel();

        if ( $data_list ){
            foreach ( $data_list as $key => $value ){
                $where = array(
                    'count_time' => $key,
                    'count_type' => CmsDataCountDayTypeConfig::user_read_avg_list
                );

                $value = $value ? : array();

                $data = array(
                    'count_time'  => $key ,
                    'count_type'  => CmsDataCountDayTypeConfig::user_read_avg_list,
                    'data'        => CommonFun::json_encode2( $value ),
                );

                // 检测数据是否已经添加
                $id = $countModel->field('id')->where( $where )->find();


                if ( $id ) {
                    $countModel->update_data( $id['id'], $data);
                } else {
                    $countModel->add_data( $data );
                }

                echo $key.'数据同步完成'.PHP_EOL;
            }
        }
    }

    /**
     * 设置币种文章排行
     */
    public function set_coin_article_hot()
    {
        $PythonPushRedis = PythonPushRedis::getInstance();
        $list = $PythonPushRedis->coin_article_hot();
        var_dump( $list );
    }

}